﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.ComponentModel;

/// <summary>
/// rank 的摘要说明
/// </summary>
public class rank
{
    public static string constr118 = getsql.getstr("gamestr");
    public static string levelcon = getsql.getstr("levelstr");
    private static string constr143 = getsql.getstr("searchstr");
   // public static string constr=System.Configuration.ConfigurationSettings.AppSettings["activestr"].ToString();
  //  public static string levelcon = System.Configuration.ConfigurationSettings.AppSettings["levelstr"].ToString();
    protected string dastr = DateTime.Now.DayOfWeek.ToString();
    protected string dahour = DateTime.Now.Hour.ToString();

    protected static volatile System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache;


    public rank()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    //判断是否领奖
    public bool IsGetReward(int useridx, int gid, int type)
    {
        int state = 0;
        SqlParameter[] pars ={
                       SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                       SqlHelper.MakeInParam("@gid",SqlDbType.Int,4,gid),
                       SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type),
                       SqlHelper.MakeOutParam("@state",SqlDbType.Int,4),
                        SqlHelper.MakeInParam("@multitype",SqlDbType.Int,4,1)
        };
        SqlHelper.ExecuteNonQuery(constr118, CommandType.StoredProcedure, "IsGetReward", pars);
        state = Convert.ToInt32(pars[3].Value);
        if (state == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    //----------------------------------------------------------------
    //得到抽奖信息
    public string GetLastWeek(int type)
    {
        string result = "";
        SqlParameter[] pars ={ 
                          SqlHelper.MakeInParam("@type",type)
        };
        DataTable dat = SqlHelper.ExecuteDataTable(constr118, CommandType.StoredProcedure, "chou_record_order", pars);
        if (dat != null && dat.Rows.Count > 0)
        {
            int len = dat.Rows.Count;
            StringBuilder strbuil = new StringBuilder();
            for (int k = 0; k < len; k++)
            {
                int star = Convert.ToInt32(dat.Rows[k]["stars"]);
                int useridx = Convert.ToInt32(dat.Rows[k]["useridx"]);
                int giftid = Convert.ToInt32(dat.Rows[k]["giftid"]);
                strbuil.AppendFormat("<tr><td>{0}</td><td class='fb'>{1}{2}</td><td class='fb'>{3}</td><td>", k + 1, GetCupImg(star), dat.Rows[k]["giftname"].ToString(), dat.Rows[k]["username"].ToString());
                SqlParameter[] npars ={ 
                                SqlHelper.MakeInParam("@useridx",useridx),
                                SqlHelper.MakeInParam("@giftid",giftid)
                };
                DataTable ndat = SqlHelper.ExecuteDataTable(constr118, CommandType.StoredProcedure, "chou_lastweek", npars);
                if (ndat != null && ndat.Rows.Count > 0)
                {
                    int nlen = ndat.Rows.Count;
                    for (int m = 0; m < nlen; m++)
                    {
                        strbuil.Append(GetRewardImg(ndat.Rows[m]["rewardName"].ToString()));
                    }
                }
                strbuil.Append("</td></tr>");
            }

            result = strbuil.ToString();
        }

        return result;

    }

    public string GetRewardImg(string name)
    {
        string result = "";
        switch (name)
        {
            case "现金3000元":
                result = "<img src='common/images/ar1.jpg'>";
                break;
            case "现金1500元":
                result = "<img src='common/images/br1.jpg'>";
                break;
            case "现金2000元":
                result = "<img src='common/images/ar2.jpg'>";
                break;
            case "现金1000元":
                result = "<img src='common/images/br2.jpg'>";
                break;
            case "现金500元":
                result = "<img src='common/images/br3.jpg'>";
                break;
            case "法拉利458 Italia":
                result = "<img src='common/images/ar8.jpg'>";
                break;
            case "路虎":
                result = "<img src='common/images/br8.jpg'>";
                break;
            case "玛莎拉蒂 GT":
                result = "<img src='common/images/ar9.jpg'>";
                break;
            case "悍马 H2":
                result = "<img src='common/images/br9.jpg'>";
                break;
            case "日产 GTR":
                result = "<img src='common/images/ar10.jpg'>";
                break;
            case "宝马 X6":
                result = "<img src='common/images/br10.jpg'>";
                break;
            case "80W货币":
                result = "<img src='common/images/ar4.jpg'>";
                break;
            case "40W货币":
                result = "<img src='common/images/br4.jpg'>";
                break;
            case "60W货币":
                result = "<img src='common/images/ar5.jpg'>";
                break;
            case "30W货币":
                result = "<img src='common/images/br5.jpg'>";
                break;
            case "50W货币":
                result = "<img src='common/images/ar6.jpg'>";
                break;
            case "20W货币":
                result = "<img src='common/images/br6.jpg'>";
                break;
            case "15W货币":
                result = "<img src='common/images/br7.jpg'>";
                break;

        }
        return result;
    }
    //=-------------------------------------

    public string GetOtherRank(int type, int giftid)
    {
        SqlParameter[] pars ={ SqlHelper.MakeInParam("@type", SqlDbType.Int, 4, type), SqlHelper.MakeInParam("@giftid", SqlDbType.Int, 4, giftid) };
        DataTable dat = SqlHelper.ExecuteDataTable(constr118, CommandType.StoredProcedure, "Get_Other_Histry", pars);
        string str = "";
        if (dat != null)
        {
            StringBuilder strbuil = new StringBuilder();
            int len = dat.Rows.Count;
            for (int k = 0; k < len; k++)
            {
                string cla = "";
                if (k == 0)
                {
                    cla = "fc01";
                }
                string myname = dat.Rows[k]["myname"].ToString();
                    myname = myname.Replace("м", "");
                strbuil.AppendFormat("<li><span class='l_w1 {4}'>{0}</span><span class='l_w2'><a title='{1}'>{2}</a></span><span class='l_w3'>{3}</span></li>", k + 1, dat.Rows[k]["useridx"], myname, dat.Rows[k]["num"],cla);
            }

            str = strbuil.ToString();
        }
        return str;
    }

    //得到历史排名
    public string GetHistoryRank(int giftid, int type)
    {
        SqlParameter[] pars ={ SqlHelper.MakeInParam("@giftid", SqlDbType.Int, 4, giftid) };
        DataTable dt = SqlHelper.ExecuteDataTable(constr118, CommandType.StoredProcedure, "Get_History_Rank", pars);
        string str = "";
        if (dt != null && dt.Rows.Count > 0)
        {
            StringBuilder sb = new StringBuilder();
            int len = dt.Rows.Count;
            len = len > 3 ? 3 : len;
            for (int i = 0; i < len; i++)
            {
                //if (type == 1)
                //{
                //    string cla = "";
                //    if (i == 0)
                //    {
                //        cla = "fc01";
                //    }
                //    sb.AppendFormat("<li><span class='l_w1 {4}'>{0}</span><span class='l_w2'><a title='{1}'>{2}</a></span><span class='l_w3'>{3}</span></li>", i + 1, dt.Rows[i]["useridx"], dt.Rows[i]["myname"], dt.Rows[i]["num"], cla);
                //}
                //if (type == 2)
                //{
                //    string kinstr = "";
                //    int knum = pccheck(dt.Rows[i]["useridx"].ToString(), giftid);
                //    for (int s = 0; s < knum; s++)
                //    {
                //        kinstr += "<span class='jb1'></span>";
                //    }
                //    if (knum == 3)
                //    {
                //        kinstr = "<span class='jb2'></span>";
                //    }
                //    sb.AppendFormat("<li><span class='sbt_num'>{0}</span><span class='sbt_na'><a title='{1}'>{2}</a></span><span><span>{3}次</span></span></li>", i + 1, dt.Rows[i]["useridx"], dt.Rows[i]["myname"], dt.Rows[i]["num"]);
                //}
                int knum = pccheck(dt.Rows[i]["useridx"].ToString(), giftid);
                sb.Append("<li>");
                sb.AppendFormat("<span class=\"sbt_num\">{0}</span>", i+1);
                sb.AppendFormat("<span class=\"sbt_na\"><a href=\"#\" title=\"{0}\">{1}</a></span>", dt.Rows[i]["useridx"], dt.Rows[i]["myname"]);
                sb.AppendFormat("<span class=\"sbt_va\"><span>{0}次</span>{1}</span>", dt.Rows[i]["num"], GetCupImg(knum));
                sb.Append("</li>");

            }
            str = sb.ToString();
        }
        return str;
    }

    //是否有青蛙图标
    public string levelsearch(int useridx, int sex)
    {

        DataTable metable = null;
        string stagepic = "";
        SqlParameter[] p =
					{
						SqlHelper.MakeInParam("@useridx", SqlDbType.Int,4, useridx),
					};
        metable = SqlHelper.ExecuteDataTable(levelcon, CommandType.StoredProcedure, "f_userleve_search", p);
        if (metable.Rows.Count > 0)
        {
            int stage = int.Parse(metable.Rows[0]["userrank"].ToString());
            int stagevalue = int.Parse(metable.Rows[0]["curexp"].ToString());
            int nexvalue = int.Parse(metable.Rows[0]["nextexp"].ToString());
            if (stage <= 19)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa11.gif'' align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl1.gif'  align='absmiddle' />";
            }
            if (stage > 19 && stage <= 39)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa22.gif'    align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl2.gif'   align='absmiddle' />";

            }
            if (stage > 39 && stage <= 59)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa33.gif'   align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl3.gif'  width='20px' height='21px'  align='absmiddle' />";

            }
            if (stage > 59 && stage <= 79)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa44.gif'   align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl4.gif'    align='absmiddle' />";

            }
            if (stage > 79 && stage <= 99)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa55.gif'   align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl5.gif'    align='absmiddle' />";

            }
            if (stage == 100)
            {
                if (sex == 1)
                    stagepic = "<img src='common/images/level/wa66.gif'    align='absmiddle' />";
                else
                    stagepic = "<img src='common/images/level/wa_girl6.gif'   align='absmiddle' />";

            }
           string titlestr = "等级：" + stage + ";经验：" + stagevalue + "/" + nexvalue;
            stagepic = "<a title='" + titlestr + "'>" + stagepic + "</a>";
        }
        return stagepic;
    }

    //得到连冠杯次数
    public int Concheck(string useridx, int giftid)
    {
        int count = 0;
        int sendidx = Convert.ToInt32(useridx);
        string sql = "exec f_pro_continuchek " + sendidx + "," + giftid;
        try
        {
            //				DataSet ds=getds(sql);
            //				DataTable dt=ds.Tables[0];
            //				count=int.Parse(dt.Rows[0][0].ToString());
            SqlParameter[] p =
					{
						SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,int.Parse(useridx)),
						SqlHelper.MakeInParam("@giftid",SqlDbType.Int,4,giftid),
						SqlHelper.MakeInParam("@multitype",SqlDbType.Int,4,1)
						//					@useridx int,                          ------用户编号
						//@giftid  int,							------道具编号
						//@multitype int
					};
            DataTable metable = SqlHelper.ExecuteDataTable(constr118, CommandType.StoredProcedure, "f_pro_continuchek", p);
            count = int.Parse(metable.Rows[0][0].ToString());
        }
        catch
        {
        }

        return count;
    }

    //得到所有礼物标签
    public DataSet getpds(int gift, int type)
    {
        SqlParameter[] p =
				{
					SqlHelper.MakeInParam("@giftid",SqlDbType.Int,4,gift),
					SqlHelper.MakeInParam("@type",SqlDbType.Int,4,type)
				};
        DataSet pds = SqlHelper.ExecuteDataset(constr118, CommandType.StoredProcedure, "f_propect_search", p);
        return pds;
    }

    //得到道具及数据
    public string getstar(int giftid, int type)
    {
        string useridx = "";
        string userid = "";
        string username = "";
        string gnum = "";
        int knum = 0;
        string restr = "";
        string kinstr = "";
        DataSet ds = getpds(giftid, type);
        if (ds.Tables.Count > 0)
        {
            StringBuilder sb = new StringBuilder();
            DataTable dt = ds.Tables[0];
            DataRow[] dr = dt.Select("", "outquantity desc");
            if (dr != null && dr.Length > 0)
            {
                if (type == 1)
                {
                    useridx = dr[0]["useridx"].ToString();
                    userid = dr[0]["id"].ToString();
                    username = dr[0]["myname"].ToString();
                    gnum = dr[0]["outquantity"].ToString();
                 //   if (dastr == "Sunday" && dahour == "23")
                    int min = DateTime.Now.Minute;
                    if (dahour == "23" || (dahour == "0" && min < 10))
                        gnum = "";
                    //knum = Concheck(useridx, giftid);
                    //string expnum = "";
                    //for (int s = 0; s < knum; s++)
                    //{
                    //    kinstr += "<span class='jb1'></span>";
                    //    if (knum == 2)
                    //        expnum = "1600";
                    //    if (knum == 1)
                    //        expnum = "800";
                    //}
                    //if (knum == 3)
                    //{
                    //    kinstr = "<span class='jb2'></span>";
                    //    expnum = "6000";
                    //}
                    //if (knum == 1 || knum==0)
                    //{
                    //    expnum = "800";
                    //}
                    //if (knum == 2)
                    //{
                    //    expnum = "1600";
                    //}
                    //if (knum == 3)
                    //{
                    //    if (giftid == 35 || giftid == 36 || giftid == 102)
                    //        expnum = "6000";
                    //    else
                    //    {
                    //        if(giftid==81||giftid== 27||giftid== 10||giftid== 14||giftid== 9||giftid== 8||giftid==16||giftid== 11 )
                    //        expnum = "3000";
                    //        else
                    //            expnum="2500";
                    //    }
                    //}
                    //string str = "";
                    //if (IsGetReward(Convert.ToInt32(useridx), giftid, 2))
                    //{
                    //    str = "<li><span class='s_qr'>已领</span></li>";
                    //}
                    //strbuil.AppendFormat("<li><span>昵称：<a title='{0}'>{1}</a></span></li><li><span>数量：{2}</span>{3}</li><li><span>{4}</span><span class='s_jy'>" + expnum + "点</span></li>{5}", useridx, username, gnum, kinstr, levelsearch(Convert.ToInt32(useridx), 1),str);
                    sb.AppendFormat("<div class=\"pc_lname t_hid\">用户：<b class=\"f13\" title=\"{0}\">{1}</b></div><div class=\"pc_lname t_hid\">数量：<b class=\"f13\">{2}</b></div>", useridx, username, gnum);

                }
                if (type == 2)
                {
                    int rowlength = 3;
                    if (dr.Length < 3)
                        rowlength = dr.Length;
                    for (int i = 0; i < rowlength; i++)
                    {
                        kinstr = "";
                        useridx = dr[i]["useridx"].ToString();
                        userid = dr[i]["id"].ToString();
                        username = dr[i]["myname"].ToString();
                        gnum = dr[i]["outquantity"].ToString()+"次";
                        
                        if (dastr == "Sunday" && dahour == "23")
                            gnum = "排行中...";
                        
                        knum = Concheck(useridx, giftid);

                        sb.AppendFormat("<li><span class='sbt_num'>{0}</span><span class='sbt_na'><a title='{1}'>{2}</a></span><span class='sbt_va'><span>{3}</span>{4}</span></li>", i + 1, useridx, username, gnum, GetCupImg(knum));
                    }
                }
            }
            restr = sb.ToString();
            if (giftid == 33 || giftid == 18)
                restr = "";
        }
        return restr;
    }

    //拼接印章和鉴定
    public string getpr(int giftid, int type)
    {
        string useridx = "";
        string userid = "";
        string username = "";
        string gnum = "";
        int knum = 0;
        string restr = "";
        string kinstr = "";
        DataSet ds = getpds(giftid, type);
        if (ds.Tables.Count > 0)
        {
            StringBuilder strbuil = new StringBuilder();
            if (type == 3 || type == 5)
                giftid = 0;
            else
                giftid = 999;
            DataTable dt = ds.Tables[0];
            DataRow[] dr = dt.Select();
            if (type == 3 || type == 4)
            {
                useridx = dr[0]["useridx"].ToString();
                userid = dr[0]["id"].ToString();
                username = dr[0]["myname"].ToString();
                
                if (type == 4)
                    gnum = dr[0]["gcash"].ToString();
                else
                    gnum = dr[0]["gnum"].ToString();
                
                if (dastr == "Sunday" && dahour == "23")
                    gnum = "排行中...";
                
                knum = pccheck(useridx, giftid);

                strbuil.AppendFormat("<li><span>昵称：<a title='{0}'>{1}</a></span></li><li><span>数量：{2}</span>{3}</li><li><span>{4}</span><span class='s_jy'>200点</span></li>", useridx, username, gnum, GetCupImg(knum), levelsearch(Convert.ToInt32(useridx), 1));
            }
            if (type == 5 || type == 6)
            {
                int rowlength = 3;
                if (dr.Length < 3)
                    rowlength = dr.Length;
                for (int i = 0; i < rowlength; i++)
                {
                    kinstr = "";
                    useridx = dr[i]["useridx"].ToString();
                    userid = dr[i]["id"].ToString();
                    username = dr[i]["myname"].ToString();
                    gnum = dr[i]["gnum"].ToString() + "次";
                    
                    if (dastr == "Sunday" && dahour == "23")
                        gnum = "排行中...";
                    
                    knum = pccheck(useridx, giftid);

                    strbuil.AppendFormat("<li><span class='sbt_num'>{0}</span><span class='sbt_na'><a title='{1}'>{2}</a></span><span class='sbt_va'><span>{3}</span>{4}</span></li>", i + 1, useridx, username, gnum, GetCupImg(knum));

                }
            }
            restr = strbuil.ToString();
        }
        return restr;
    }

    //得到印章和鉴定的idx及type
    public int pccheck(string useridx, int giftid)
    {
        int count = 0;
        int sendidx = Convert.ToInt32(useridx);
        string sql = "exec f_print_continuchek " + sendidx + "," + giftid;
        try
        {
            //				DataSet ds=getds(sql);
            //				DataTable dt=ds.Tables[0];
            //				count=int.Parse(dt.Rows[0][0].ToString());
            SqlParameter[] p =
					{
						SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,int.Parse(useridx)),
						SqlHelper.MakeInParam("@giftid",SqlDbType.Int,4,giftid)
						//					@useridx int,                          ------用户编号
						//@giftid  int,							------道具编号
						//@multitype int
					};
            DataTable metable = SqlHelper.ExecuteDataTable(constr118, CommandType.StoredProcedure, "f_print_continuchek", p);
            count = int.Parse(metable.Rows[0][0].ToString());
        }
        catch
        {
        }

        return count;

    }

    public string getconsume(int type)
    {
        string roomid = "";
        string roomname = "";
        string gnum = "";
        string restr = "";
        DataSet ds = getcds(type);
        if (ds.Tables.Count > 0)
        {
            DataTable dt = ds.Tables[0];
            DataRow[] dr = dt.Select();
            StringBuilder strbuil = new StringBuilder();
            for (int i = 0; i < dr.Length; i++)
            {
                string cla = "";
                if (i % 2 == 1)
                {
                    cla = "class='bcg02'";
                }
                if (type == 1)
                {
                    roomid = dr[i]["roomid"].ToString();
                    roomname = dr[i]["name"].ToString();
                    gnum = dr[i]["roomconsume"].ToString();
                    strbuil.AppendFormat("<tr {4}><td class='numbg'>{0}</td><td><a href='http://room.9158.com/in_user_roomin.aspx?roomid="+roomid+"&type=1' target='_blank'>{1}</a></td><td><a href='http://room.9158.com/in_user_roomin.aspx?roomid="+roomid+"&type=1' target='_blank'>{2}</a></td><td>{3}</td></tr>", i + 1, roomid, roomname, gnum, cla);

                }
                if (type == 2)
                {
                    roomid = dr[i]["roomid"].ToString();
                    roomname = dr[i]["name"].ToString();
                    gnum = dr[i]["avp"].ToString();
                    strbuil.AppendFormat("<tr {4}><td class='numbg'>{0}</td><td><a href='http://room.9158.com/in_user_roomin.aspx?roomid="+roomid+"&type=1' target='_blank'>{1}</a></td><td><a href='http://room.9158.com/in_user_roomin.aspx?roomid="+roomid+"&type=1' target='_blank'>{2}<a></td><td>{3}</td></tr>", i + 1, roomid, roomname, gnum, cla);
                }
                //if (type == 3)
                //{
                //    roomid = dr[i]["useridx"].ToString();
                //    roomname = dr[i]["myname"].ToString();
                //    gnum = dr[i]["outquantity"].ToString();
                //    restr += "<li><span class='b_num'>" + (i + 1) + "</span><span class='b_na'><a  title='" + roomid + "'>" + roomname + "</a></span>";
                //    restr += "<span class='b_va'>" + gnum + "</span></li>";
                //}

            }
            restr = strbuil.ToString();
        }
        return restr;
    }

    public DataSet getcds(int type)
    {
        string sql = "";
        DataSet meds = null;
        if (type == 1)
        {
            SqlParameter[] p ={ };
            sql = "f_roomconsume_list";
            meds = SqlHelper.ExecuteDataset(constr118, CommandType.StoredProcedure, sql, p);
        }
        if (type == 2)
        {
            string idate1 = DateTime.Now.ToString();
            sql = "f_kroomavp";
            SqlParameter[] p ={ SqlHelper.MakeInParam("@idate", SqlDbType.DateTime, 20, idate1) };
            meds = SqlHelper.ExecuteDataset(constr143, CommandType.StoredProcedure, sql, p);
        }
        //if (type == 3)
        //{
        //    SqlParameter[] p ={ };
        //    sql = "f_day_consumelist";
        //    meds = SqlHelper.ExecuteDataset(constr, CommandType.StoredProcedure, sql, p);
        //}
        return meds;
    }

    /// <summary>
    ///  得到奖杯图片
    /// </summary>
    /// <param name="num"></param>
    /// <returns></returns>
    public string GetCupImg(int num)
    {
        string result = string.Empty;
        switch (num)
        {
            case 1:
                result = "<span class='jb1'></span> ";
                break;
            case 2:
                result = "<span class='jb1'></span><span class='jb1'></span>";
                break;
            case 3:
                result = "<span class='jb2'></span>";
                break;
        }
        result = "";
        return result;
    }

    #region 新版普通周星 2012.11.12 新增

    /// <summary>
    /// 获取当前周次
    /// </summary>
    /// <returns></returns>
    public int GetWeekStarIndex()
    {
        SqlParameter[] pars = new SqlParameter[1];
        pars[0] = new SqlParameter("@TypeId", SqlDbType.Int);
        pars[0].Value = 1;

        DataTable dt = SqlHelper.ExecuteDataTable(constr143, CommandType.StoredProcedure, "TenRoom_Gift_WeekStar_All", pars);
        return Convert.ToInt32(dt.Rows[0][0]);
    }

    /// <summary>
    /// 根据礼物ID获取当天该礼物的总数
    /// </summary>
    /// <param name="giftId"></param>
    /// <returns></returns>
    public int GetDayOfGiftNum(int giftId)
    {
        int iRet = 0;
        SqlParameter[] pars = new SqlParameter[2];
        pars[0] = new SqlParameter("@TypeId", SqlDbType.Int);
        pars[0].Value = 2;
        pars[1] = new SqlParameter("@GiftId", SqlDbType.Int);
        pars[1].Value = giftId;

        DataTable dt = SqlHelper.ExecuteDataTable(constr143, CommandType.StoredProcedure, "TenRoom_Gift_WeekStar_All", pars);
        if (dt != null && dt.Rows.Count > 0)
        {
            if (!Convert.IsDBNull(dt.Rows[0][0]))
            {
                iRet = Convert.ToInt32(dt.Rows[0][0]);
            }
        }
        
        return iRet;             
    }

    public DataTable GetDayStarInfo(int giftId)
    {
        SqlParameter[] pars = new SqlParameter[2];
        pars[0] = new SqlParameter("@TypeId", SqlDbType.Int);
        pars[0].Value = 5;
        pars[1] = new SqlParameter("@GiftId", SqlDbType.Int);
        pars[1].Value = giftId;

        return SqlHelper.ExecuteDataTable(constr143, CommandType.StoredProcedure, "TenRoom_Gift_WeekStar_All", pars);
         
    }

    /// <summary>
    /// 判断用户是否已经填了表单信息
    /// </summary>
    /// <param name="useridx"></param>
    /// <returns></returns>
    public bool IsHaveForm(int useridx)
    {
        SqlParameter[] pars ={ 
                   SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                   SqlHelper.MakeOutParam("@formid",SqlDbType.Int,4)
        };
        SqlHelper.ExecuteNonQuery(constr118, CommandType.StoredProcedure, "chou_IsHaveForm", pars);
     
        int iRet = Convert.ToInt32(pars[1].Value);
        if (iRet > 0)
            return true;
        else
            return false;
    }

    /// <summary>
    /// 增加领奖人信息
    /// </summary>
    /// <returns></returns>
    public int InsertAwardInfo(int useridx, string userid, string userqq, string usertel, string realname, string bankdress, string bankid, int giftId)
    {
        SqlParameter[] p =
                    {
                        SqlHelper.MakeInParam("@useridx",SqlDbType.Int,4,useridx),
                        SqlHelper.MakeInParam("@userid",SqlDbType.VarChar,20,userid),
                        SqlHelper.MakeInParam("@userqq",SqlDbType.VarChar,20,userqq),
                        SqlHelper.MakeInParam("@usertel",SqlDbType.VarChar,20,usertel),
                        SqlHelper.MakeInParam("@type",SqlDbType.Int,4,1),
                        SqlHelper.MakeInParam("@realname",SqlDbType.VarChar,30,realname),
                        SqlHelper.MakeInParam("@bankdress",SqlDbType.VarChar,50,bankdress),
                        SqlHelper.MakeInParam("@bankid",SqlDbType.VarChar,20,bankid),
                        SqlHelper.MakeInParam("@giftId",SqlDbType.Int,4,giftId)
                    };

        DataTable dt = SqlHelper.ExecuteDataTable(constr118, CommandType.StoredProcedure, "f_proget_apply", p);

        return Convert.ToInt32(dt.Rows[0][0]);
    }

    /// <summary>
    /// 每日夺宝星领奖
    /// </summary>
    /// <returns>返回值 1：领取成功 -1：未获奖或已领取  -2：金币增加失败</returns>
    public int DayStarAward(int giftId, int userIdx)
    {
        SqlParameter[] pars = new SqlParameter[3];
        pars[0] = new SqlParameter("@TypeId", SqlDbType.Int);
        pars[0].Value = 7;
        pars[1] = new SqlParameter("@GiftId", SqlDbType.Int);
        pars[1].Value = giftId;
        pars[2] = new SqlParameter("@UIdx", SqlDbType.Int);
        pars[2].Value = userIdx;

        DataTable dt = SqlHelper.ExecuteDataTable(constr143, CommandType.StoredProcedure, "TenRoom_Gift_WeekStar_All", pars);
        return Convert.ToInt32(dt.Rows[0][0]);
    }

    /// <summary>
    /// 根据礼物ID获取当天该礼物的总数(Form Cache)
    /// </summary>
    /// <param name="giftId"></param>
    /// <returns></returns>
    public int GetDayOfGiftNumCache(int giftId)
    {
        string key = "DayOfGiftNum";
        int GiftNum = Convert.ToInt32(webCache[key]);
        if (GiftNum == 0)
        {
            GiftNum = GetDayOfGiftNum(giftId);
            webCache.Insert(key, GiftNum, null, DateTime.Now.AddMinutes(1), TimeSpan.Zero);
        }

        return GiftNum;
    }

    /// <summary>
    /// 获取当前周次
    /// </summary>
    /// <returns></returns>
    public int GetWeekIndex()
    {
        string key = "GiftWeekIndex";
        int weekIndex = Convert.ToInt32(webCache[key]);
        if (weekIndex == 0)
        {
            weekIndex = GetWeekStarIndex();
            webCache.Insert(key, weekIndex, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);

        }

        return weekIndex;
    }

    /// <summary>
    /// 获取周礼物ID数组
    /// </summary>
    /// <param name="weekIndex"></param>
    /// <returns></returns>
    public int[] GetWeekOfGiftArry(int weekIndex)
    {

        int[] arry = null;
        switch (weekIndex)
        {
            case 1:
                arry = new int[] { (int)GiftType.花仙子, (int)GiftType.幸运红包, (int)GiftType.牛人之星, (int)GiftType.爱神丘比特 };
                break;
            case 2:
                arry = new int[] { (int)GiftType.小糊涂仙, (int)GiftType.香水之星, (int)GiftType.乌龟之星, (int)GiftType.好好先生 };
                break;
            case 0:
            case 3:
                arry = new int[] { (int)GiftType.棒棒糖星, (int)GiftType.四叶草, (int)GiftType.幸运雪茄, (int)GiftType.荧光棒星 };
                break;
        }
        return arry;

        #region
        //string key = "GIFT_WeekStar_Set";
        //DataTable dt = webCache[key] as DataTable;
        //if (dt == null || dt.Rows.Count == 0)
        //{
        //    rank rank = new rank();
        //    dt = rank.GetWeekStarSet();

        //    webCache.Insert(key, dt, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero);
        //}

        //DataRow[] drs = dt.Select(string.Format("status={0}", 1));
        //if (drs != null && drs.Length > 0)
        //{
        //    int i = 0; ThisWeekGiftArry = new int[drs.Length];
        //    foreach (DataRow dr in drs)
        //    {
        //        ThisWeekGiftArry[i] = Convert.ToInt32(dr["GiftId"]);

        //        i++;
        //    }
        //}

        //drs = dt.Select(string.Format("status={0}", 0));
        //if (drs != null && drs.Length > 0)
        //{
        //    int i = 0; LastWeekGiftArry = new int[drs.Length];
        //    foreach (DataRow dr in drs)
        //    {
        //        ThisWeekGiftArry[i] = Convert.ToInt32(dr["GiftId"]);

        //        i++;
        //    }
        //}
        #endregion
    }

    /// <summary>
    /// 获取获得每日夺宝星的用户IDX
    /// </summary>
    /// <returns></returns>
    public int GetUIdxOfDayStar(int giftId)
    {
        int iRet = 0;
        DataTable dt = GetDayStarInfo(giftId);
        if (dt != null && dt.Rows.Count > 0)
        {
            if (!Convert.IsDBNull(dt.Rows[0]["uidx"]))
            {
                iRet = Convert.ToInt32(dt.Rows[0]["uidx"]);
            }
        }

        return iRet;
    }

    /// <summary>
    /// 获取每日夺宝星礼物ID
    /// </summary>
    /// <returns></returns>
    public int GetDayOfGiftId(int[] GiftArry)
    {
        int dayIndex = (int)DateTime.Now.DayOfWeek;
      /*  dayIndex = dayIndex > 7 ? 1 : dayIndex;
        if (dayIndex == 0)
            dayIndex = 7;*/
dayIndex = dayIndex == 0 ? 7 : dayIndex;
        int iRet = 1;
        if (dayIndex >= 5)
        {
            iRet = Convert.ToInt32(GiftArry[dayIndex - 5]);
        }
        else
        {
            iRet = Convert.ToInt32(GiftArry[dayIndex - 1]);
        }
        return iRet;
    }
    #endregion




}
public enum GiftType
{
    
    /// <summary>
    /// 花仙子 1
    /// </summary>
    [Description("500万")]
    花仙子 = 1,
    /// <summary>
    /// 啤酒 8
    /// </summary>
    [Description("150万")]
    小糊涂仙 = 8,
    /// <summary>
    /// 牛人之星 9
    /// </summary>
    [Description("30000")]    
    牛人之星 = 9,
    /// <summary>
    /// 棒棒糖星 10
    /// </summary>   
    [Description("350万")]    
    棒棒糖星 = 10,
    /// <summary>
    /// 乌龟之星 16
    /// </summary>
    [Description("30000")]    
    乌龟之星 = 16,
    /// <summary>
    ///  幸运雪茄 314
    /// </summary>
    [Description("80万")]    
    幸运雪茄 = 314,
    /// <summary>
    /// 幸运红包 31
    /// </summary>
    [Description("8万")]   
    幸运红包 = 31,
    /// <summary>
    ///  爱神丘比特 36
    /// </summary>
    [Description("15000")]    
    爱神丘比特 = 36,
    /// <summary>
    /// 荧光棒星 39
    /// </summary>
    [Description("20万")]    
    荧光棒星 = 39,
    /// <summary>
    /// 好好先生 102
    /// </summary>
    [Description("450")]   
    好好先生 = 102,
    /// <summary> 
    /// 香水之星 68
    /// </summary>
    [Description("45万")]    
    香水之星 = 68,
    /// <summary>
    /// 阿拉丁神灯 312  四叶草 313
    /// </summary>
    [Description("150万")]
    四叶草 = 313
}
